layui.use(['table', 'admin', 'ax', 'func', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    let laydate = layui.laydate;

    laydate.render({
        elem: '#activeDate'
        ,type: 'date'
        , range: '至' //或 range: '~' 来自定义分割字符
    });
    /**
     * 参加活动人员表管理
     */
    var Member = {
        tableId: "memberTable"
    };

    /**
     * 初始化表格的列
     */
    Member.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'memberId', hide: false, title: '志愿者ID'},
            {field: 'memberName', sort: true, title: '姓名'},
            {field: 'sex', sort: true, title: '性别'},
            {field: 'age', sort: true, title: '年龄'},
            {field: 'address', sort: true, title: '现居住地'},
            {field: 'tel', sort: true, title: '电话'},
            {field: 'idCard', sort: true, title: '身份证号码'},
            {field: 'hours', sort: true, title: '总服务时长（小时）'},
            {field: 'school', sort: true, title: '学校/单位'},
            {field: 'registerDate', sort: true, title: '注册时间'},
            {field: 'createTime', hide: true, title: ''},
            {field: 'updateTime', hide: true, title: ''},
            {field: 'createUser', hide: true, title: ''},
            {field: 'updateUser', hide: true, title: ''},
            {field: 'delFlag', hide: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Member.search = function () {
        var queryData = {};
        queryData['memberName'] = $("#memberName").val();
        queryData['school'] = $("#school").val();
        table.reload(Member.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Member.openAddDlg = function () {
        func.open({
            title: '添加参加活动人员表',
            content: Feng.ctxPath + '/member/add',
            tableId: Member.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Member.openEditDlg = function (data) {
        func.open({
            title: '修改参加活动人员表',
            content: Feng.ctxPath + '/vlt/member/edit?id=' + data.id,
            tableId: Member.tableId
        });
    };
    Member.onActiveItem = function (data) {
        func.open({
            title: '参加活动信息',
            content: Feng.ctxPath + '/activeMember/index2?memberId=' + data.memberId,
            tableId: Member.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    Member.exportExcel = function () {
        let form = $("<form></form>").attr("action", Feng.ctxPath + '/vlt/member/export').attr("method", "post");
        form.append($("<input></input>").attr("type", "hidden").attr("name", "memberName").attr("value", $('#memberName').val()));
        form.append($("<input></input>").attr("type", "hidden").attr("name", "school").attr("value", $('#school').val()));
        form.appendTo('body').submit().remove();
        // var checkRows = table.checkStatus(Member.tableId);
        // if (checkRows.data.length === 0) {
        //     Feng.error("请选择要导出的数据");
        // } else {
        //     table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        // }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Member.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/member/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Member.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Member.tableId,
        url: Feng.ctxPath + '/vlt/member/list',
        page: true,
        height: "full-150",
        cellMinWidth: 100,
        cols: Member.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Member.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Member.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Member.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Member.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Member.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Member.onDeleteItem(data);
        } else if (layEvent === 'active') {
            Member.onActiveItem(data);
        }
    });
});
